//
//  HYDatabaseManager.m
//  airpop
//
//  Created by yan on 2016/11/29.
//  Copyright © 2016年 aetheris. All rights reserved.
//

#import "HYDatabaseManager.h"

@implementation HYDatabaseManager

+ (HYDatabaseManager *)shareInstance {
    return [HYDatabaseManager allocWithZone:NULL];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static id manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [super allocWithZone:zone];
    });
    return manager;
}

+ (void)executeSqls:(NSArray<NSString *> *)sqls finished:(void (^) (BOOL result))finished {
    dispatch_async([HYDatabaseManager shareInstance].queue, ^{
        FMDatabase *db = [HYDatabaseManager database];
        if (db) {
            [db beginTransaction];
            for (NSString *sql in sqls) {
                [db executeUpdate:sql];
            }
            [db commit];
        }
        [db close];
        dispatch_async(dispatch_get_main_queue(), ^{
            finished ? finished(YES) : nil;
        });
    });
}

+ (void)executeSql:(NSString *)sql finished:(void (^) (BOOL result))finished {
    [HYDatabaseManager executeSqls:@[sql] finished:finished];
}

+ (void)selectSql:(NSString *)sql class:(Class)cla keys:(NSArray<NSString *> *)keys finished:(void (^) (NSArray *results))finished {
    dispatch_async([HYDatabaseManager shareInstance].queue, ^{
        FMDatabase *db = [HYDatabaseManager database];
        FMResultSet *result = nil;
        if (db) {
            result = [db executeQuery:sql];
        }
        NSMutableArray *results = [NSMutableArray array];
        while ([result next]) {
            id model = [[cla alloc] init];
            for (NSString *key in keys) {
                id value = [result objectForColumnName:key];
                if (!value) {
                    continue;
                }
                [model setValue:value forKey:key];
            }
            [results addObject:model];
        }
        [db close];
        dispatch_async(dispatch_get_main_queue(), ^{
            finished ? finished(results) : nil;
        });
    });
}

+ (void)selectSql:(NSString *)sql keys:(NSArray<NSString *> *)keys finished:(void (^) (NSArray *results))finished {
    dispatch_async([HYDatabaseManager shareInstance].queue, ^{
        FMDatabase *db = [HYDatabaseManager database];
        FMResultSet *result = nil;
        if (db) {
            result = [db executeQuery:sql];
        }
        NSMutableArray *results = [NSMutableArray array];
        while ([result next]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            for (NSString *key in keys) {
                id value = [result objectForColumnName:key];
                [dic setObject:value forKey:key];
            }
            [results addObject:[dic copy]];
        }
        [db close];
        dispatch_async(dispatch_get_main_queue(), ^{
            finished ? finished([results copy]) : nil;
        });
    });
}

+ (FMDatabase *)database {
    FMDatabase *db = [FMDatabase databaseWithPath:[HYDatabaseManager path]];
    if ([db open]) {
        return db;
    }
    return nil;
}

+ (NSString *)path {
    if ([HYDatabaseManager shareInstance].path) {
        return [HYDatabaseManager shareInstance].path;
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString *path = paths.firstObject;
    path = [path stringByAppendingPathComponent:@"fmdb_default.sqlite"];
    return path;
}

- (dispatch_queue_t)queue {
    if (!_queue) {
        _queue = dispatch_queue_create("fmdb_queue", DISPATCH_QUEUE_SERIAL);
    }
    return _queue;
}

@synthesize queue = _queue;
@end
